<template>
  <div class="mainbox">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="菜品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="菜品分类" prop="categoryId">
        <el-select v-model="ruleForm.categoryId" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜品价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="菜品图片" prop="image">
        <el-upload
          class="avatar-uploader"
          :action="`${env}/common/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="ruleForm.image"
            :src="`${env}/common/download?name=${ruleForm.image}`"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="菜品描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.push({ name: 'Employee' })">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCategoryList, addDishes ,editDishes,getDishesById,} from "@/api/dishes";
export default {
  data() {
    return {
      options: [],
      env: process.env.VUE_APP_BASE_API,
      ruleForm: {
        code: "",
        name: "",
        price: "",
        image: "",
        description: "",
        categoryId: "",
        flavors: [],
      },
      rules: {
        name: [{ required: true, message: "请输入菜品名称", trigger: "blur" }],
        categoryId: [{ required: true, trigger: "blur" }],
        price: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        image: [{ required: true, trigger: "change" }],
      options: [],
      },
    };
  },
  created() {
    getCategoryList({ type: "1" }).then((res) => {
      this.options = res.data;
    });
  },
  methods: {
    init() {
      if (this.isEdit) {
        getDishesById(this.$route.query.id).then((res) => {
          this.ruleForm = res.data;
        });
      }
      getCategoryName().then((res) => {
        this.options = res.data;
      });
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.image = res.data;
    },
    handleSave(){
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            await editDishes(this.ruleForm);
          } else {
            await addDishes(this.ruleForm);
          }
          this.$message.success("保存成功");
          this.$router.push({ name: "Dishes" });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isEdit() {
      return this.$route.query.isEdit;
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
</style>

</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
.el-form {
  width: 500px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
